package com.diligent.cloud.sfc.service.impl;

import com.canny.commons.model.Codes;
import com.canny.commons.model.ListResult;
import com.diligent.cloud.primary.data.model.Result;
import com.diligent.cloud.sfc.service.DpsService;
import com.diligent.cloud.workorder.model.DpsListParams;
import com.diligent.cloud.workorder.model.WoDpsMaster;
import com.diligent.cloud.workorder.service.WoDpsFeignService;
import com.diligent.cloud.workorder.service.WoDpsMasterFeignService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * DpsServiceImpl class
 * DPS 工单列表
 *
 * @author xf107278
 * @date 6/12/2020
 */
@Service
public class DpsServiceImpl implements DpsService {
    private final WoDpsFeignService woDpsFeignService;

    private final WoDpsMasterFeignService dpsMasterFeignService;

    public DpsServiceImpl(WoDpsFeignService woDpsFeignService, WoDpsMasterFeignService dpsMasterFeignService) {
        this.woDpsFeignService = woDpsFeignService;
        this.dpsMasterFeignService = dpsMasterFeignService;
    }

    @Override
    public ResponseEntity<ListResult> selectDpsList(DpsListParams dpsListParams) {
        ResponseEntity<Result> resultResponseEntity = woDpsFeignService.getList(dpsListParams);

        ListResult listResult;
        if (resultResponseEntity.getBody() != null) {
            List<Map> maps = (List<Map>) resultResponseEntity.getBody().getData();
            listResult = new ListResult(maps);
            listResult.modifyCode(Codes.success.getCode(), String.valueOf(resultResponseEntity.getBody().getMsg()));
        } else {
            listResult = new ListResult();
            listResult.modifyCode(Codes.fail.getCode(), Codes.fail.getMsg());
        }
        return ResponseEntity.ok(listResult);
    }

    @Override
    public ResponseEntity<Result> updateDps(Long dpsId) {
        WoDpsMaster woDpsMaster = new WoDpsMaster();
        woDpsMaster.setId(dpsId);
        woDpsMaster.setRemark("已上架");
        return dpsMasterFeignService.update(woDpsMaster);
    }
}
